﻿using System;
using System.Data.SqlClient;
using System.Windows.Forms;


namespace Mes_app
{
    public partial class Users_add_Form : Form
    {
        public Users_add_Form()
        {
            InitializeComponent();
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            string textuserid = TextUserID.Text.ToString();
            string textusername = TextUserName.Text.ToString();
            string textpassword = TextPassWord.Text.ToString();
            string textpasswordok = TextPassWordOK.Text.ToString();


            if (textuserid.Equals(""))
            {
                MessageBox.Show("用户ID不能为空！");
                return;
            }

            if (textusername.Equals(""))
            {
                MessageBox.Show("用户名不能为空！");
                return;
            }
            if (textpassword.Equals(""))
            {
                MessageBox.Show("用户密码不能为空！");
                return;
            }
            if (textpasswordok.Equals(""))
            {
                MessageBox.Show("确认密码不能为空！");
                return;
            }
            if (!textpassword.Equals(textpasswordok))
            {
                MessageBox.Show("密码和确认密码不一致！");
                return;
            }


            //查询userid是否存在
            string sqlValue = "SELECT count(*) FROM login_user WHERE login_user_id = @userid ";
            SqlParameter[] parameters = {
                  new SqlParameter("@userid",textuserid)
            };

            int count = (int)SqlHelper.ExecuteScalar(sqlValue, parameters);
            Console.WriteLine(count);

            //   int affectCount = SqlHelper.ExecuteScalar(sqlValue, parameters);
            //   Console.WriteLine("影响行数：" + affectCount);

            if (count > 0)
            {
                label_show.Text = "用户名已经存在";
            }
            else
            {
                string sqlinsert = "INSERT INTO login_user (login_user_id,login_user_name,login_user_password) " +
                    " VALUES(@userid,@textusername,@textpassword) ";
                SqlParameter[] param_insert = {
                  new SqlParameter("@userid",textuserid),
                  new SqlParameter("@textusername",textusername),
                  new SqlParameter("@textpassword",textpassword),
                };

                int count_id = SqlHelper.ExecuteNonQuery(sqlinsert, param_insert);

                label_show.Text = "用户名" + count_id + "添加成功";

                TextUserID.Text = "";
                TextUserName.Text = "";
                TextPassWord.Text = "";
                TextPassWordOK.Text = "";
            }

        }

        private void Button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Register_Load(object sender, EventArgs e)
        {
            TextPassWord.Focus();
        }
    }

}
